package cmd

import (
	"context"
	"github.com/go-redis/redis/v8"
	"github.com/spf13/cobra"
	"log"
)

var addr string
var password string

var redisCmd = &cobra.Command{
	Use:   "redis",
	Short: "redis cluster check tool",
	Long: `redis cluster check tool,it's will write redis key'`,
	Run: func(cmd *cobra.Command, args []string)  {
		// Do Stuff Here
		log.Println("redis command run")
		runRedisCommand(addr,password)
	},
}

func init() {
	redisCmd.Flags().StringVarP(&addr, "addr", "a", "", "redis cluster urls")
	redisCmd.Flags().StringVarP(&password, "password", "p", "", "redis password")

	rootCmd.AddCommand(redisCmd)
}

func runRedisCommand(addr string,password string)  {

	rdb := redis.NewClient(&redis.Options{
		Addr:     addr,
		Password: password, // no password set
		DB:       0,  // use default DB
	})
	_, err := rdb.Ping(context.Background()).Result()
	if err != nil {
		log.Fatal(err)
	}

	//var list map[string]string
	//list = make(map[string]string)
	//addrs := strings.Split(addr, ",")
	//for i := range addrs {
	//	host := strings.Split(addrs[i], ":")
	//	list[host[0]] = host[1]
	//}
	//log.Println(list)
	//rdb := redis.NewRing(&redis.RingOptions{
	//	Addrs: list,
	//	Password: password, // no password set
	//	DB:       0,  // use default DB
	//})
	//_, err := rdb.Ping(context.Background()).Result()
	//if err != nil {
	//	log.Fatal(err)
	//}

	log.Println("ok")
}


